package com.meila.cms.controller;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.meila.cms.auth.AuthClass;
import com.meila.cms.auth.AuthMethod;
import com.meila.cms.entity.AjaxObj;
import com.meila.cms.entity.Car;
import com.meila.cms.entity.CarInstructionsDesign;
import com.meila.cms.entity.PartListDesign;
import com.meila.cms.model.CarDesignModel;
import com.meila.cms.model.CarInstructionsDesignModel;
import com.meila.cms.model.PartListDesignModel;
import com.meila.cms.services.ICarDesignService;
import com.meila.cms.services.ICarInstructionsService;
import com.meila.cms.services.IPartListDesignService;
import com.meila.cms.util.json.JsonUtil2;
import com.meila.cms.util.page.Pager;

@Controller
@RequestMapping("/front/carDesign")
@AuthClass("login")
public class CarDesignController extends BaseController{
	
	private ICarDesignService carDesignService;
 
	private IPartListDesignService partListDesignService;
	
	private ICarInstructionsService instructionsListDesignService;
	
	public ICarDesignService getCarDesignService() {
		return carDesignService;
	}
	
	@Inject
	public void setCarDesignService(ICarDesignService carDesignService) {
		this.carDesignService = carDesignService;
	}

	
	
	public IPartListDesignService getPartListDesignService() {
		return partListDesignService;
	}

	@Inject
	public void setPartListDesignService(
			IPartListDesignService partListDesignService) {
		this.partListDesignService = partListDesignService;
	}

 
	public ICarInstructionsService getInstructionsListDesignService() {
		return instructionsListDesignService;
	}
	@Inject
	public void setInstructionsListDesignService(
			ICarInstructionsService instructionsListDesignService) {
		this.instructionsListDesignService = instructionsListDesignService;
	}

	/**
	 * 获取零件分页列表集合信息
	 * 
	 * @param model
	 * @return 获取零件分页列表集合信息
	 */
	@RequestMapping(value="/carDesign_list", method = RequestMethod.GET)
	public String listCarDesign(CarDesignModel carDesignModel,Model model) {
		 
		// 获取用户分页列表集合信息
		Pager<CarDesignModel> listCarDesig = carDesignService.listCarDesign(carDesignModel);
		// 保存用户分页列表集合信息
		model.addAttribute("datas", listCarDesig);
		
		model.addAttribute("jsondatas",JsonUtil2.toJSONList(listCarDesig.getDatas()));
		
		return "carDesign/carDesign_list";
	}
	////////////////////////////////////////////////////////////////////
	//新增,修改整车设计
	
	/**
	 * 保存修改logo后的整车设计字段
	 * @param userModel
	 * @return
	 */
	@RequestMapping("/updateCarDesign1")
	@ResponseBody
	public AjaxObj updateCarDesign1(CarDesignModel carDesignModel) {
		carDesignModel.setUpdatetime(new Date());
		AjaxObj ajaxObj=carDesignService.updateCarDesign1(carDesignModel, request);
		
		return ajaxObj;
		  
	}
 
	
	
	/**
	 * 未保存过车型的更新
	 * @param userModel
	 * @return
	 */
	@RequestMapping("/updateCarDesign2")
	@ResponseBody
	public AjaxObj updateCarDesign2(CarDesignModel carDesignModel) {
		carDesignModel.setUpdatetime(new Date());
		AjaxObj ajaxObj=carDesignService.updateCarDesign2(carDesignModel, request);
		
		return ajaxObj;
		  
	}
 
	
	/**
	 * 保存过车型的更新
	 * @param userModel
	 * @return
	 */
	@RequestMapping("/updateCarDesign3")
	@ResponseBody
	public AjaxObj updateCarDesign3(CarDesignModel carDesignModel) {
		carDesignModel.setUpdatetime(new Date());
		AjaxObj ajaxObj=carDesignService.updateCarDesign3(carDesignModel, request);
		
		return ajaxObj;
		  
	}
 
	
	@RequestMapping("/delCarDesign")
	@ResponseBody
	public AjaxObj delCarDesign(CarDesignModel carDesignModel) {
	 
		AjaxObj ajaxObj=carDesignService.delCarDesign(carDesignModel);
		
		return ajaxObj;
		  
	}
 
 
	/**
	 * 跳转到输入整车设计名界面
	 * @param model
	 * @return
	 */
	@RequestMapping("/newDesign")
	public String newDesign(Model model) {
 
		return "carDesign/newDesign";
	}
 
	
    /**
     * 跳转到修改界面
     * @param carDesignMode
     * @param model
     * @return
     */
	@RequestMapping("/carDesignModify")
	public String carDesignModify(CarDesignModel carDesignModel,Model model) {
 
        carDesignService.carDesignToModify(carDesignModel,model);
        
		return "carDesign/carDesignModify";
	}
	
	
    /**
     * 新增整车设计后,跳转到选择车型界面
     * @param carDesignMode
     * @param model
     * @return
     */
	@RequestMapping("/addCarDesign")
	public String addCarDesign(CarDesignModel carDesignModel,Model model) {
 
        carDesignService.addCarDesign(carDesignModel,model);
        
		return "carDesign/carDesignModify";
	}
	
	
	/**
	 * 根据车型获取车辆列表
	 * @param carDesignMode
	 * @return
	 */
	@RequestMapping("/getCarList")
	@ResponseBody
	public List getCarList(CarDesignModel carDesignModel) {
 
        List list = carDesignService.getCarList(carDesignModel);
        
		return list;
	}
	
	
	 
	/**
	 * 跳转到导出整车设计2D图pdf页面
	 * @param designid
	 * @param model
	 * @return
	 */
	@RequestMapping("/carDesignpdf")
	public String carDesignpdf(Integer designid,Model model) {
 
		CarDesignModel carDesignModel=carDesignService.carDesignByid(designid);
		
		model.addAttribute("carDesignModel", carDesignModel);
 
	    return "carDesign/carDesignpdf";
	}
	
 
	
	@RequestMapping("/carLogoModify")
	public String carLogoModify(CarDesignModel carDesignModel,Model model) {
 
        carDesignService.carLogoModify(carDesignModel,model);
		
/*      //去缓存
        response.setHeader("Cache-Control","no-store");
        response.setHeader("Pragrma","no-cache");
        response.setDateHeader("Expires",0);*/
        
		return "carDesign/carLogoModify";
	}
	
	////////////////////////////////////////////////////////////////////
	//零件清单列表
	@RequestMapping("/partList")
	public String partList(PartListDesign partListDesign,Model model) {
 
		PartListDesignModel partListDesignModel=partListDesignService.partListDesign(partListDesign);
		
		model.addAttribute("partListDesignModel", partListDesignModel);
		
		return "carDesign/partList";
	}
	
	/**
	 * 添加整车设计零件清单里的零件
	 * @param partListDesignModel
	 * @param model
	 * @return
	 */
	@RequestMapping("/addPartListDesign")
	public String addPartListDesign(PartListDesignModel partListDesignModel) {
 
		 partListDesignService.addPartListDesign(partListDesignModel);
 
		
		return "redirect:/front/carDesign/partList?designid="+partListDesignModel.getDesignid();
	}
	
	/**
	 * 删除零件清单的零件
	 */
	@RequestMapping("/delpart")
	@ResponseBody
	public AjaxObj delpart(String ids){
		
		AjaxObj  ajaxObj= partListDesignService.delpart(ids);
		
		return ajaxObj;
	}
	
	
	/**
	 * 保存零件清单的零件
	 */
	@RequestMapping("/saveEditPart")
	@ResponseBody
	public AjaxObj saveEditPart(@RequestBody  List<JSONObject> partListDesignModels){
		
		AjaxObj  ajaxObj= partListDesignService.saveEditPart(partListDesignModels);
		
		return ajaxObj;
	}
	

	
 
	/**
	 * 跳转到导出零件清单pdf页面
	 * @param partListDesign
	 * @param model
	 * @return
	 */
	@RequestMapping("/partListpdf")
	public String partListpdf(PartListDesign partListDesign,Model model) {
 
		PartListDesignModel partListDesignModel=partListDesignService.partListDesign(partListDesign);
		
		model.addAttribute("partListDesignModel", partListDesignModel);
 
		 return "carDesign/partListpdf";
	}
	
	@RequestMapping("/exportPDF")
	public ResponseEntity<byte[]> exportPDF(String designid,int type){
		ResponseEntity<byte[]> result = null;
	
		if(type==1){
			result = partListDesignService.exportPDF(designid,request,response);
		}else if(type==2){
			result = instructionsListDesignService.exportPDF(designid,request,response);
		}else if(type==3){
			result = carDesignService.exportPDF(designid,request,response);
		}
	
  
        return result;  
	 
	}
	
	////////////////////////////////////////////////////////////////////////////
	//工艺指令列表
	@RequestMapping("/craftOrder")
	public String craftOrder(CarInstructionsDesign carInstructions,Model model) {
 
		CarInstructionsDesignModel carInstructionsModel=instructionsListDesignService.craftOrderList(carInstructions);
		
		model.addAttribute("carInstructionsModel", carInstructionsModel);
		
		
		return "carDesign/craftOrder";
	}
	
	/**
	 * 添加工艺指令
	 * @param partListDesignModel
	 * @param model
	 * @return
	 */
	@RequestMapping("/addInstrictionList")
	public String addInstrictionList(CarInstructionsDesignModel carInstructionsModel) {
 
		instructionsListDesignService.addCarInstructions(carInstructionsModel);
 
		
		return "redirect:/front/carDesign/craftOrder?designid="+carInstructionsModel.getDesignid();
	}
	/**
	 * 删除工艺指令
	 */
	@RequestMapping("/delCarInstructions")
	@ResponseBody
	public AjaxObj delCarInstructions(String ids,String designid){
		
		AjaxObj  ajaxObj= instructionsListDesignService.delCarInstructions(ids, designid);
		
		return ajaxObj;
	}
	/**
	 * 保存零件清单的零件
	 */
	@RequestMapping("/saveEditInstructions")
	@ResponseBody
	public AjaxObj saveEditInstructions(@RequestBody  List<JSONObject> instructionsModels){
		
		AjaxObj  ajaxObj= instructionsListDesignService.saveEditInstructions(instructionsModels);
		
		return ajaxObj;
	}
	
	/**
	 * 跳转到导出零件清单pdf页面
	 * @param partListDesign
	 * @param model
	 * @return
	 */
	@RequestMapping("/craftOrderpdf")
	public String craftOrderpdf(CarInstructionsDesign carInstructions,Model model) {
 
		 CarInstructionsDesignModel carInstructionsModel=instructionsListDesignService.craftOrderList(carInstructions);
		
		 model.addAttribute("carInstructionsModel", carInstructionsModel);
 
		 return "carDesign/craftOrderpdf";
	}
    ////////////////////////////////////////////////////////////////////
	//选择性下载文件
	@RequestMapping("/downloadFileBychose")
	public 	  ResponseEntity<byte[]>  downloadFileBychose(String designid,Integer[] chose) {
		
		ResponseEntity<byte[]> result = null;
	 
	    result = carDesignService.downloadFileBychose(designid,chose,request,response);
 
        return result;  
		
	}

 
	
	/**
	 * 根据车型id检查剩余空间是否够上传
	 * @param fileids
	 * @param isupdate
	 * @return
	 */
	@RequestMapping("/checkMemory")
	@ResponseBody
	public AjaxObj  checkMemory(Integer carid,Boolean isupdate){
		
		AjaxObj ajaxObj;
		Car car=carMapper.selectByPrimaryKey(carid);
 
		Integer[] fileids={car.getCarafterpic(),car.getCarfrontpic(),car.getCarsidepic(),car.getPlanid(),car.getThreedid()};
		
		ajaxObj=carDesignService.updateMemory(fileids,isupdate);
 
		return ajaxObj;
	}
}
